import Setting from "@/config/setting";
import Log from "./Log";

const cookies = {};

// #ifdef H5
import Cookies from "js-cookie";
/**
 * @description 存储 cookie 值
 *
 */
cookies.set = function(name = "default", value = "", cookieSetting = {}) {
  let currentCookieSetting = {
    expires: Setting.cookiesExpires,
  };
  Object.assign(currentCookieSetting, cookieSetting);
  Cookies.set(`admin-${name}`, value, currentCookieSetting);
};

/**
 * @description 拿到 cookie 值
 * @param {String} name cookie name
 */
cookies.get = function(name = "default") {
  return Cookies.get(`admin-${name}`);
};

/**
 * @description 拿到 cookie 全部的值
 */
cookies.getAll = function() {
  return Cookies.get();
};

/**
 * @description 删除 cookie
 * @param {String} name cookie name
 */
cookies.remove = function(name = "default") {
  return Cookies.remove(`admin-${name}`);
};
// #endif

// #ifdef MP-ALIPAY
/**
 * @description 存储 cookie 值
 *
 */
cookies.set = function(name = "default", value = "") {
  uni.setStorage({
    key: `admin-${name}`,
    data: value,
    fail: (error) => {
      if (Setting.ENV === "developemt") {
        Log.error(error);
      }
    },
  });
};

/**
 * @description 拿到 cookie 值
 * @param {String} name cookie name
 */
cookies.get = function(name = "default") {
  try {
    const value = uni.getStorageSync(`admin-${name}`);
    if (value) {
      return value;
    }
  } catch (e) {
    // error
  }
};

/**
 * @description 拿到 cookie 全部的值
 */
cookies.getAll = function() {
  return Cookies.get();
};

/**
 * @description 删除 cookie
 * @param {String} name cookie name
 */
cookies.remove = function(name = "default") {
    uni.removeStorage({
        key: `admin-${name}`
    });
};

// #endif

export default cookies;
